/*
 * ZBrowser is an embeddable browser component.
 * Copyright (C) Author: Gangadhar Nagesh Metla (Novell, Inc.)
 * nmetla74@gmail.com 
 * Version 1.0
 * 1/3/2009
 * Filename ZURLStreamHandler.java
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.novell.zenworks.agent.zbrowser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLStreamHandler;
import java.rmi.NotBoundException;
import java.util.Locale;

import com.novell.zenworks.zicon.common.HTMLPage;

public class ZURLStreamHandler extends URLStreamHandler
{
    ZURLConnection   urlc   = null;
    HTMLPage         page   = null;

    public ZURLStreamHandler(HTMLPage page)
    {
        this.page = page;
    }

    protected ZURLConnection openConnection(URL u) throws IOException
    {
        String html = null;
        try
        {
            if(page.getType().equalsIgnoreCase("property"))
            {
                html = NavigationUtils.getServer().getPropertyPage(page.getId(),Locale.getDefault().getLanguage(),Locale.getDefault().getCountry());
            }
            else if(page.getType().equalsIgnoreCase("dataform"))
            { 
                html = NavigationUtils.getServer().getFormControls(page.getId());
            }
        } catch (NotBoundException e)
        {
            throw new IOException(e.getMessage());
        }
        byte[] htmlBytes= html.getBytes("utf-8");

        this.urlc = new ZURLConnection(u, (new ByteArrayInputStream(htmlBytes)));
        return urlc;
    }

}
